package com.huarun.module.upload;

import java.io.IOException;

import com.huarun.common.cos.PicUploadService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.huarun.common.BaseController;
import com.huarun.common.ResponseMessage;
import com.huarun.common.util.Constant;
import com.huarun.common.util.FastDFSUtil;
import com.huarun.common.util.FtpUtil;
import com.huarun.common.util.MyException;
import com.huarun.common.util.ResultCodeValue;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author hqy
 * @创建时间   2018-08-13 11:33
 * @Description 类描述
 */
@Controller
@RequestMapping("/wx/upload")
@Api(description = "文件上传")
public class UploadController extends BaseController {
//	@Autowired
//	FastDFSUtil fastDFSUtil;
	@Autowired
	FtpUtil ftpUtil;
	@Autowired
	private PicUploadService picUploadService;

//	@ApiOperation(notes = "文件上传", value = "文件上传")
//	@RequestMapping(value = "/upload", method = RequestMethod.POST)
//	@ResponseBody
//	public Object upload(MultipartFile file) {
//		if (file == null) {
//			throw new MyException(ResultCodeValue.R_PARAM_REQUIRED, "上传文件不能为空");
//		}
//		if (file.getSize() == 0) {
//			throw new MyException(ResultCodeValue.R_PARAM_REQUIRED, "上传文件不能为空");
//		}
//		ResponseMessage<String> responseMessage = new ResponseMessage<>();
//		try {
//			responseMessage.setData(fastDFSUtil.upload(file));
//			log.info("上传成功！");
//		} catch (IOException e) {
//			ResponseMessage.failureMessage(ResultCodeValue.R_SYSTEM_CODE_ERROR, "上传失败！" + e.getMessage(), null);
//			log.error("上传失败！");
//		}
//		return responseMessage;
//	}

	@ApiOperation(notes = "ftp文件上传", value = "ftp文件上传")
	@RequestMapping(value = "/ftpUpload", method = RequestMethod.POST)
	@ResponseBody
	public Object ftpUpload(MultipartFile file) {
		if (file == null) {
			throw new MyException(ResultCodeValue.R_PARAM_REQUIRED, "上传文件不能为空");
		}
		if (file.getSize() == 0) {
			throw new MyException(ResultCodeValue.R_PARAM_REQUIRED, "上传文件不能为空");
		}
		ResponseMessage<String> responseMessage = new ResponseMessage<>();
		String fileName = ftpUtil.uploadFile(file);
		if (StringUtils.isBlank(fileName)) {
			ResponseMessage.failureMessage(ResultCodeValue.R_SYSTEM_CODE_ERROR, "上传失败！", null);
		} else {
			responseMessage.setData(Constant.FTP_VISITOR_IP + fileName);
		}
		return responseMessage;
	}

}
